// Include the libraries we need #include #include #include #include LiquidCrystal_I2C lcd(0x3F, 16, 2); #define VOLTAGE 5.00 //system voltage #define OFFSET -260 //zero drift voltage #define LED 13 //operating instructions #define ONE_WIRE_BUS 12 //Temp Sensor datawire connected to arduino pin equivalent 12 #define T2 11 double orpValue; #define ArrayLenth 40 //times of collection #define orpPin 1 //orp meter output,connect to Arduino controller ADC pin // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); OneWire myWire(T2); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); DallasTemperature sensores(&myWire); int orpArray[ArrayLenth]; int orpArrayIndex=0; int in1 = 7; int DoseT = 6; int Dosemv = 5; float factor = 0; float agua = 0; const int Trigger = 9; //Pin digital 9 para el Trigger del sensor const int Echo = 10; //Pin digital 10 para el Echo del sensor double avergearray(int* arr, int number){ int i; int max,min; double avg; long amount=0; if(number<=0){ printf("Error number for the array to avraging!/n"); return 0; } if(number<5){ //less than 5, calculated directly statistics for(i=0;imax){ amount+=max; //arr>max max=arr[i]; }else{ amount+=arr[i]; //min<=arr<=max } }//if }//for avg = (double)amount/(number-2); }//if return avg; } // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. Wire.begin(); Serial.begin(9600); lcd.init(); // initialize the lcd sensors.begin(); // Start up the temp sensor library sensores.begin(); pinMode(LED,OUTPUT); // Set inbuilt LED to output pinMode(DoseT, OUTPUT); pinMode(Dosemv, OUTPUT); // Print a message to the LCD. lcd.backlight(); lcd.setCursor(0,0); //Set Relay pins to output and turn them on pinMode(in1, OUTPUT); digitalWrite(in1, LOW); pinMode(Trigger, OUTPUT); //pin como salida pinMode(Echo, INPUT); //pin como entrada digitalWrite(Trigger, LOW);//Inicializamos el pin con 0 } // the loop function runs over and over again forever void loop() { float t; //timepo que demora en llegar el eco float d; //distancia en centimetros digitalWrite(Trigger, HIGH); delayMicroseconds(10); //Enviamos un pulso de 10us digitalWrite(Trigger, LOW); t = pulseIn(Echo, HIGH); //obtenemos el ancho del pulso d = t/59; //escalamos el tiempo a una distancia en cm static unsigned long orpTimer=millis(); //analog sampling interval static unsigned long printTime=millis(); if(millis() >= orpTimer) { orpTimer=millis()+20; orpArray[orpArrayIndex++]=analogRead(orpPin); //read an analog value every 20ms if (orpArrayIndex==ArrayLenth) { orpArrayIndex=0; } orpValue=((30*(double)VOLTAGE*1000)-(75*avergearray(orpArray, ArrayLenth)*VOLTAGE*1000/1024))/75-OFFSET; //convert the analog value to orp according the circuit } if(millis() >= printTime) //Every 800 milliseconds, print a numerical, convert the state of the LED indicator { printTime=millis()+800; digitalWrite(LED,1-digitalRead(LED)); } sensors.requestTemperatures(); sensores.requestTemperatures(); agua = (2.04-(d-4)*0.2); lcd.clear(); lcd.setCursor(0, 0); lcd.print("ORP:"); lcd.print((int)orpValue); lcd.print("mV"); lcd.print(" "); lcd.print(agua); lcd.print("L"); lcd.setCursor(0,1); lcd.print("T1:"); lcd.print(sensors.getTempCByIndex(0)); lcd.print("T2:"); lcd.print(sensores.getTempCByIndex(0)); factor = 25; /* if (factor <= 0){ factor = 25;} else { factor = factor;}*/ if(sensores.getTempCByIndex(0) < factor) { digitalWrite(in1, HIGH);} else { digitalWrite(in1, LOW);} if((agua < 1.9) && (sensors.getTempCByIndex(0) < 25)){ digitalWrite(DoseT, HIGH);} else { digitalWrite(DoseT, LOW);} if((agua < 1.9) && ((int)orpValue < 300)){ digitalWrite(Dosemv, HIGH);} else { digitalWrite(Dosemv, LOW);} delay(300); }